package com.zbs.framework.common.utils;


import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

public class ResultUtil {
    public static ResultData ok(){
        return new ResultData<>().code(ErrEnum.SUCCESS.getErrCode()).message(ErrEnum.SUCCESS.getErrMsg()).data("");
    }

    public static <R> ResultData<PageData<R>> ok(PageData<R> data){
        return new ResultData<PageData<R>>().code(ErrEnum.SUCCESS.getErrCode()).data(data).message(ErrEnum.SUCCESS.getErrMsg());
    }


    public static <T> ResultData<T> ok(Object data){
        return new ResultData()
                .code(ErrEnum.SUCCESS.getErrCode())
                .data(data)
                .message(ErrEnum.SUCCESS.getErrMsg());
    }

    public static <T> ResultData<T> ok(ResultData data){
        if (data == null) {
            return ok();
        }
        return new ResultData()
                .code(data.getCode())
                .data(data.getData())
                .message(data.getMessage());
    }

    public static <R> ResultData<PageData<R>> ok(IPage<R> page){
        return new ResultData<PageData<R>>()
                .code(ErrEnum.SUCCESS.getErrCode())
                .data(new PageData<>(page.getRecords(),page.getTotal()))
                .message(ErrEnum.SUCCESS.getErrMsg());
    }

    public static <R> ResultData<List<R>> ok(List<R> data){
        return new ResultData<List<R>>().code(ErrEnum.SUCCESS.getErrCode()).data(data).message(ErrEnum.SUCCESS.getErrMsg());
    }

    public static ResultData error(ErrEnum errEnum) {
        return new ResultData<>().code(errEnum.getErrCode()).message(errEnum.getErrMsg()).data("");
    }

    public static ResultData error(String msg) {
        return new ResultData<>().code(ErrEnum.SYS_ERR.getErrCode()).message(msg).data("");
    }

    public static ResultData error() {
        return new ResultData<>().code(ErrEnum.SYS_ERR.getErrCode()).message(ErrEnum.SYS_ERR.getErrMsg());
    }

}
